package com.example.myapplication.main

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.myapplication.databinding.MainFgBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.hades.corelib.base.view.CoreDialogFragment
import org.hades.corelib.base.vm.CoreViewModel

@AndroidEntryPoint
class MainFg: CoreDialogFragment() {

    private lateinit var viewBinding: MainFgBinding
    private val viewModel: MainViewModel by viewModels()

    override fun initByViewBind(inflater: LayoutInflater, container: ViewGroup?): View {
        viewBinding = MainFgBinding.inflate(layoutInflater)
        return viewBinding.root
    }

    override fun onGetViewModel(): CoreViewModel {
        return viewModel
    }

    override fun onCreateViews() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                viewModel.uiFlow
                    .collectLatest{
                        viewBinding.tvText.text = it.str
                    }
            }
        }
    }

    fun initViewEvent() {
        viewBinding.motionBase.setOnClickListener {
            viewModel.getCash()
        }
    }
}